package com.akami.admin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import xframe.ctl.XDispatchAction;
import xframe.util.db.Row;
import xframe.util.db.Transaction;

public class CodeMasterAction extends XDispatchAction {
	public ActionForward move( 	ActionMapping       mapping,
			ActionForm          form,
			HttpServletRequest  request,
			HttpServletResponse response ) throws Exception { 

		Row	req			=	getRequestInfo(request);
		
		
		
		
		return mapping.findForward((String) req.get("NEXT_PAGE") );
	}
	
	// 리스트
	public ActionForward selectCode( 	ActionMapping       mapping,
			ActionForm          form,
			HttpServletRequest  request,
			HttpServletResponse response ) throws Exception {

		Row	req			=	getRequestInfo(request);
		//#################################################################
		//##권한 체크
		Row messageMap = new Row();
		messageMap.put("nextPage", request.getContextPath());
		messageMap.put("errorMessage", "세션이 종료 되었거나 잘못된 접근입니다.");
		messageMap.put("beforePage", request.getHeader( "REFERER"));
		messageMap.put("link_name", "로그인");
		messageMap.put("parameter", req);		
		 		
		if(SessionManager.sessioncheck(request, response, req,messageMap)){ 
			request.setAttribute("messageMap", messageMap);	
			return mapping.findForward( "sessionError" );
		}
		//#################################################################
			

		try{
			// 디비연결 시작
			Transaction.start(request);		
			
			HttpSession session = request.getSession();
			req.put("s_name", session.getAttribute("name"));
			req.put("s_email", session.getAttribute("email"));
			
	        // 전체 게시물 갯수
			String totalQueryName	=	"codeMaster.codeCnt";		
			String	totalQuery 	=	mappingQuery(request, totalQueryName);
			
			simpleExecuteQueryGetOneRow(request, totalQuery, totalQueryName);
			
			Row	SUM_LIST	=	(Row)request.getAttribute(totalQueryName);			
			req.put("TOTAL", SUM_LIST.getString("TOTAL"));					
			int start_row 	=	0;
			int page		=	0;
			//페이지 파라미터 점검
			//첫페이지는 파라미터가 없으므로 0이나 1로 셋팅한다.
			if(req.get("page") ==null || ((String)req.get("page")).trim().length() == 0 ){
				page	=	1;
			}else  {
				try{
					page	=	Integer.parseInt(((String)req.get("page")));
				}catch(Exception e){
					page	=	1;
				}
			}
			start_row	=	(page	-	1)	*	10;
			req.put("start_row", start_row+"");
			req.put("page", page+"");
			
			//전체게시물의 갯수를 토대로 다시 쿼리를 날려서 원하는 데이터만 받아온다.		
			String 	listQueryName	=	"codeMaster.selectCode";
			String	listQuery 		=	mappingQuery(req, listQueryName);	
	
			simpleExecuteQuery(request, listQuery, listQueryName);												
	
			//디비연결 종
	        Transaction.end(request);
        
		}catch(Exception e){
			e.printStackTrace();
			Transaction.rollback(request, response);
			req.put("system_error", e.toString());
			return mapping.findForward( "system_error" );
		}        
		
        return mapping.findForward( "selectCode" );       
	}
	
	
	
	
	public ActionForward getSelect( 	ActionMapping       mapping,
			ActionForm          form,
			HttpServletRequest  request,
			HttpServletResponse response ) throws Exception {
			Row	req			=	getRequestInfo(request);
			//#################################################################
			//##권한 체크
			//Row messageMap = new Row();
			//messageMap.put("nextPage", request.getContextPath());
			//messageMap.put("errorMessage", "세션이 종료 되었거나 잘못된 접근입니다.");
			//messageMap.put("beforePage", request.getHeader( "REFERER"));
			//messageMap.put("link_name", "로그인");
			//messageMap.put("parameter", req);		
					
			//if(SessionManager.sessioncheck(request, response, req,messageMap)){ 
			//	request.setAttribute("messageMap", messageMap);	
			//	return mapping.findForward( "sessionError" );
			//}
			//#################################################################

		try{
			// 디비연결 시작
			Transaction.start(request);		
			
			String 	listQueryName	=	req.getString("get");
			String	listQuery 		=	mappingQuery(req, listQueryName);	
			simpleExecuteQuery(request, listQuery, "selectxml");												
	
			//디비연결 종
	        Transaction.end(request);
        
		}catch(Exception e){
			e.printStackTrace();
			Transaction.rollback(request, response);
			req.put("system_error", e.getMessage());
			return mapping.findForward( "system_error" );			
		}    		
	return mapping.findForward("selectxml");
	}
	
	
	public ActionForward codeInsert( 	ActionMapping       mapping,
			ActionForm          form,
			HttpServletRequest  request,
			HttpServletResponse response ) throws Exception {
		
		Row	req			=	getRequestInfo(request);
		//#################################################################
		//##권한 체크
		Row messageMap = new Row();
		messageMap.put("nextPage", request.getContextPath());
		messageMap.put("errorMessage", "세션이 종료 되었거나 잘못된 접근입니다.");
		messageMap.put("beforePage", request.getHeader( "REFERER"));
		messageMap.put("link_name", "로그인");
		messageMap.put("parameter", req);		
				
		if(SessionManager.sessioncheck(request, response, req,messageMap)){ 
			request.setAttribute("messageMap", messageMap);	
			return mapping.findForward( "sessionError" );
		}
		//#################################################################

		try{		
		// 디비연결 시작
		Transaction.start(request);		
		
		HttpSession session = request.getSession();
		req.put("reg_email", session.getAttribute("s_email"));
		req.put("upd_email", session.getAttribute("s_email"));
		req.put("upd_name", req.get("reg_name"));
		
		
		
		String queryName	=	"codeMaster.insertCode";		
		String	query 	=	mappingQuery(req, queryName);
		simpleExecuteUpdate(request, query, queryName);		
		
		request.setAttribute("nextPage", request.getContextPath()+"/code.do?command=selectCode");

		//디비연결 종
        Transaction.end(request);
												
		}catch(Exception e){
			e.printStackTrace();
			Transaction.rollback(request, response);
			req.put("system_error", e.getMessage());
			return mapping.findForward( "system_error" );			
		} 
		
		return mapping.findForward("sendRedirect");
	}
	
	public ActionForward codeUpdate( 	ActionMapping       mapping,
			ActionForm          form,
			HttpServletRequest  request,
			HttpServletResponse response ) throws Exception {
		return mapping.findForward("selectCode");
	}
	
	public ActionForward codeDelete( 	ActionMapping       mapping,
			ActionForm          form,
			HttpServletRequest  request,
			HttpServletResponse response ) throws Exception {
		return mapping.findForward("selectCode");
	}
}
